package www.wmx.com.xssc.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import www.wmx.com.xssc.domain.doc.fj.RbtFileXsscbg;
import www.wmx.com.xssc.domain.doc.fj.ZtComUploadFile;
import www.wmx.com.xssc.domain.doc.fj.ZtComUploadFileOrigin;
import www.wmx.com.xssc.domain.doc.form_org.ZtUpmFormOrg;
import www.wmx.com.xssc.domain.doc.jcyjj.*;
import www.wmx.com.xssc.domain.doc.jsfmj.RbtKjjlJsfmjDwtjyj;
import www.wmx.com.xssc.domain.doc.jsfmj.RbtKjjlJsfmjJbxx;
import www.wmx.com.xssc.domain.doc.kjjbj.RbtKjjlKjjbjDwtjyj;
import www.wmx.com.xssc.domain.doc.kjjbj.RbtKjjlKjjbjJbxx;
import www.wmx.com.xssc.domain.doc.kjjbj.RbtKjjlKjjbjWcdw;
import www.wmx.com.xssc.domain.doc.yxntggj.RbtKjjlYxntggjJbxx;
import www.wmx.com.xssc.domain.mb.*;
import www.wmx.com.xssc.domain.vo.CommonShortXM;
import www.wmx.com.xssc.domain.vo.WcryShortXM;
import www.wmx.com.xssc.domain.vo.request.PDFRequest;
import www.wmx.com.xssc.domain.vo.request.XsscRequest;
import www.wmx.com.xssc.domain.vo.response.JCYJJResponse;
import www.wmx.com.xssc.domain.vo.response.JSFMResponse;
import www.wmx.com.xssc.domain.vo.response.KJJBJResponse;
import www.wmx.com.xssc.domain.vo.response.WcryResonse;
import www.wmx.com.xssc.domain.vo.response.threeResponse.JCYJJAllResponse;
import www.wmx.com.xssc.domain.vo.response.threeResponse.JSFMJAllResponse;
import www.wmx.com.xssc.domain.vo.response.threeResponse.KJJBAllResponse;
import www.wmx.com.xssc.domain.vo.response.threeResponse.YXNTGGAllResponse;
import www.wmx.com.xssc.domain.vo.result.R;
import www.wmx.com.xssc.service.*;
import www.wmx.com.xssc.service.fj.IRbtFileXsscbgService;
import www.wmx.com.xssc.service.fj.IZtComUploadFileOriginService;
import www.wmx.com.xssc.service.fj.IZtComUploadFileService;
import www.wmx.com.xssc.service.form_org.IZtUpmFormOrgService;
import www.wmx.com.xssc.service.jccj.IRbtKjjlJccgjJbxxService;
import www.wmx.com.xssc.service.jcyjj.*;
import www.wmx.com.xssc.service.jsfmj.IRbtKjjlJsfmjDwtjyjService;
import www.wmx.com.xssc.service.jsfmj.IRbtKjjlJsfmjJbxxService;
import www.wmx.com.xssc.service.kjjbj.IRbtKjjlKjjbjDwtjyjService;
import www.wmx.com.xssc.service.kjjbj.IRbtKjjlKjjbjJbxxService;
import www.wmx.com.xssc.service.kjjbj.IRbtKjjlKjjbjWcdwService;
import www.wmx.com.xssc.service.yxntggj.IRbtKjjlYxntggjJbxxService;
import www.wmx.com.xssc.util.common.MbReadUtil;
import www.wmx.com.xssc.util.common.WcrReadUtil;
import www.wmx.com.xssc.util.enumUtil.CgjlType;
import www.wmx.com.xssc.util.enumUtil.TjdjEnum;
import www.wmx.com.xssc.util.pdf.PDF;
import www.wmx.com.xssc.util.threeUtil.JcyjjProcessUtil;
import www.wmx.com.xssc.util.threeUtil.JsfmjProcessUtil;
import www.wmx.com.xssc.util.threeUtil.KjjbjProcessUtil;
import www.wmx.com.xssc.util.time.DateFormatterUtil;
import www.wmx.com.xssc.util.tptq.ImageExtractionUtil;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
@Slf4j
public class XsscServiceImpl implements XsscService {


    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Resource
    private ImageExtractionUtil imageExtractionUtil;

    @Resource
    private IRbtFileXsscbgService xsscbgService;

    //初始化当前指定年度的所有的完成人的基本信息:
    @Resource
    private WcrReadUtil wcrReadUtil;
    //当前年度的所有完成人员的列举：
    List<WcryShortXM> wcryShortXMList;
    @Resource(description = "模版读取工具")
    private MbReadUtil mbReadUtil;

    @Resource(description = "PDF生成工具")
    private PDF pdfUtil;

    @Resource
    private IZtComUploadFileService comUploadFileService;
    @Resource
    private IZtComUploadFileOriginService comUploadFileOriginService;


    @Resource
    private IRbtKjjlJcyjjJbxxService jcyjjJbxxService;
    @Resource
    private IRbtKjjlJsfmjJbxxService jsfmjJbxxService;
    @Resource
    private IRbtKjjlKjjbjJbxxService kjjbjJbxxService;
    @Resource(description = "一线难题攻关奖基本信息")
    private IRbtKjjlYxntggjJbxxService yxntggjJbxxService;
    @Resource(description = "杰出成就奖基本信息")
    private IRbtKjjlJccgjJbxxService jccgjJbxxService;


    @Resource(description = "服务架构部门名称")
    private IZtUpmFormOrgService formOrgService;
    @Resource(description = "基础研究奖-论文信息")
    private IRbtKjjlJcyjjLwxxService jcyjjLwxxService;
    @Resource(description = "基础研究奖专著信息")
    private IRbtKjjlJcyjjZzxxService jcyjjZzxxService;

    @Resource(description = "基础研究奖的处理工具")
    private JcyjjProcessUtil jcyjjProcessUtil;
    @Resource(description = "科技进步奖的处理工具")
    private KjjbjProcessUtil kjjbjProcessUtil;
    @Resource(description = "技术发明奖的处理工具")
    private JsfmjProcessUtil jsfmjProcessUtil;


    @Resource(description = "基础研究奖-推荐单位")
    private IRbtKjjlJcyjjDwtjyjService jcyjjDwtjyjService;
    @Resource(description = "基础研究奖-完成单位")
    private IRbtKjjlJcyjjWcdwService jcyjjWcdwService;
    @Resource(description = "技术发明奖-推荐单位")
    private IRbtKjjlJsfmjDwtjyjService jsfmjDwtjyjService;
    @Resource(description = "科技进步奖-推荐单位")
    private IRbtKjjlKjjbjDwtjyjService kjjbjDwtjyjService;
    @Resource(description = "科技进步奖-完成单位")
    private IRbtKjjlKjjbjWcdwService kjjbjWcdwService;
    @Resource(description = "技术发明奖的等级对于专利的要求")
    private IRbtKjjlMbJsfmjService mbJsfmjService;


    @Resource(description = "基础研究奖限制数量")
    private IRbtKjjlMbJcyjjNumService jcyjjNumService;
    @Resource
    private IRbtKjjlMbJsfmjNumService jsfmjNumService;
    @Resource
    private IRbtKjjlMbKjjbjNumService kjjbjNumService;
//    @Resource
//    private IRbtKjjlMbYxntggjNumService yxntggjNumService;


    /**
     * 完成人员所参加的当前年度的其他奖项的审查：
     */
    private RbtKjjlMbTy mbTyJCYJJ;
    private RbtKjjlMbTy mbTyJSFMJ;
    private RbtKjjlMbTy mbTyKJJBJ;
    List<RbtKjjlMbTy> mbTyList;
    Set<String> tjdwNameSet = new HashSet<>();
    Set<String> wcdwNameSet = new HashSet<>();


    @Override
    public R makeXssc(XsscRequest query) {

        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                makeXsscPDF(query);
            }
        });
        return new R("200", null, LocalDateTime.now(), null);
    }

    /**
     * 需要审查的每个整体是项目及其涉及到的所有的申请奖励：
     *
     * @param query
     * @return
     */

    public void makeXsscPDF(XsscRequest query) {
        //1.初始化需要用到的所有信息：
        this.wcryShortXMList = wcrReadUtil.initWcryProjectXM(query.getNd());
        jcyjjProcessUtil.getArticleProcessUtil().init();
        jcyjjProcessUtil.getZzProcessUtil().init();
        jcyjjProcessUtil.getPatentProcessUtil().init();
        mbReadUtil.init();
        for (RbtKjjlMbTy rbtKjjlMbTy : mbReadUtil.getKjjlMbTyList()) {
            if (Integer.parseInt(rbtKjjlMbTy.getFl()) == CgjlType.JCYJJ.getCode()) {
                mbTyJCYJJ = rbtKjjlMbTy;
            } else if (Integer.parseInt(rbtKjjlMbTy.getFl()) == CgjlType.JSFMJ.getCode()) {
                mbTyJSFMJ = rbtKjjlMbTy;
            } else if (Integer.parseInt(rbtKjjlMbTy.getFl()) == CgjlType.KJJBJ.getCode()) {
                mbTyKJJBJ = rbtKjjlMbTy;
            }
        }
        mbTyList = new ArrayList<>();
        mbTyList.add(mbTyJCYJJ);
        mbTyList.add(mbTyJSFMJ);
        mbTyList.add(mbTyKJJBJ);
        // 1.2 所有项目的 基础、技术发明、科技进步奖、一线难题攻关技术奖的基本信息：
        List<RbtKjjlJcyjjJbxx> jcyjjJbxxList = jcyjjJbxxService.lambdaQuery().eq(RbtKjjlJcyjjJbxx::getNd, query.getNd()).list();
        List<RbtKjjlJsfmjJbxx> jsfmjJbxxList = jsfmjJbxxService.lambdaQuery().eq(RbtKjjlJsfmjJbxx::getNd, query.getNd()).list();
        List<RbtKjjlKjjbjJbxx> kjjbjJbxxList = kjjbjJbxxService.lambdaQuery().eq(RbtKjjlKjjbjJbxx::getNd, query.getNd()).list();
        List<RbtKjjlYxntggjJbxx> yxntggjJbxxList = yxntggjJbxxService.lambdaQuery().eq(RbtKjjlYxntggjJbxx::getNd, query.getNd()).list();

        List<JCYJJAllResponse> jcyjjResponseList = new ArrayList<>();
        List<JSFMJAllResponse> jsfmResponsesList = new ArrayList<>();
        List<KJJBAllResponse> kjjbjResponseList = new ArrayList<>();
        List<YXNTGGAllResponse> yxntggResponseList = new ArrayList<>();

        Map<String, RbtKjjlMbJcyjjNum> jcyjjNumMap = jcyjjNumService.lambdaQuery().list().stream().collect(Collectors.toMap(RbtKjjlMbJcyjjNum::getDj, e -> e));
        Map<String, RbtKjjlMbJsfmjNum> jsfmjNumMap = jsfmjNumService.lambdaQuery().list().stream().collect(Collectors.toMap(RbtKjjlMbJsfmjNum::getDj, e -> e));
        Map<String, RbtKjjlMbKjjbjNum> kjjbjNumMap = kjjbjNumService.lambdaQuery().list().stream().collect(Collectors.toMap(RbtKjjlMbKjjbjNum::getDj, e -> e));
//        Map<String, RbtKjjlMbYxntggjNum> yxntggjNumMap = yxntggjNumService.lambdaQuery().list().stream().collect(Collectors.toMap(RbtKjjlMbYxntggjNum::getDj, e -> e));


        /**
         * 1.基础研究奖：
         */
        for (RbtKjjlJcyjjJbxx jcyjjJbxx : jcyjjJbxxList) {
            JCYJJAllResponse jcyjjAllResponse = new JCYJJAllResponse();
            //基础研究奖论文的推荐等级的要求：
            List<RbtKjjlJcyjjLwxx> jcyjjLwxxes = jcyjjLwxxService.lambdaQuery().eq(RbtKjjlJcyjjLwxx::getKjjlJcyjjId, jcyjjJbxx.getId()).list();
            String tjdjFailReason = checkTJDJ(jcyjjJbxx, jcyjjLwxxes);
            if (tjdjFailReason != null) {
                jcyjjAllResponse.setTjdjFailReason(tjdjFailReason);
            }
            //各个成果的检验结果：
            JCYJJResponse jcyjjResponse = jcyjjProcessUtil.processJcyjj(jcyjjJbxx.getId());
            jcyjjResponse.setJbxxId(jcyjjJbxx.getId());
//            jcyjjResponseList.add(jcyjjResponse);
            //完成人参与项目的处理：
            List<WcryResonse> wcryResonseList = new ArrayList<>();
            List<WcryShortXM> wcryShortXMS = wcrReadUtil.getWcryFilterByTypeAndId(CgjlType.JCYJJ.getCode(), jcyjjJbxx.getId());
            if (wcryShortXMS != null && !wcryShortXMS.isEmpty()) {
                for (WcryShortXM wcry : wcryShortXMS) {
                    WcryResonse wcryResonse = wcrReadUtil.checkWcry(mbTyList, CgjlType.JCYJJ.getCode(), wcry);
                    wcryResonse.setWcryShortXM(wcry);
                    if (!wcryResonse.isSuccess()) {
                        wcryResonseList.add(wcryResonse);
                    }
                }
            }
            //推荐单位和完成单位：
            List<String> dwtjyjList = jcyjjDwtjyjService.lambdaQuery().eq(RbtKjjlJcyjjDwtjyj::getKjjlJcyjjId, jcyjjJbxx.getId()).list().stream().map(e -> e.getTjdwmc()).collect(Collectors.toList());
            List<String> wcdwList = jcyjjWcdwService.lambdaQuery().eq(RbtKjjlJcyjjWcdw::getKjjlJcyjjId, jcyjjJbxx.getId()).list().stream().map(e -> e.getDwmc()).collect(Collectors.toList());

            jcyjjAllResponse.setTjdwList(dwtjyjList);
            this.tjdwNameSet.addAll(dwtjyjList);
            this.wcdwNameSet.addAll(wcdwList);
            jcyjjAllResponse.setWcdwList(wcdwList);
            jcyjjAllResponse.setJcyjjJbxx(jcyjjJbxx);
            jcyjjAllResponse.setJcyjjResponse(jcyjjResponse);
            jcyjjAllResponse.setWcryResonseList(wcryResonseList);

            String tjdj = jcyjjJbxx.getTjdj();
            tjdj = tjdj.substring(tjdj.length() - 1);
            RbtKjjlMbJcyjjNum mbJcyjjNum = jcyjjNumMap.get(tjdj);
            if (wcryShortXMS != null) {
                int sizeWCR = wcryShortXMS.size();
                int sizeWCDW = wcdwList.size();
                if (sizeWCDW > mbJcyjjNum.getDw()) {
                    jcyjjAllResponse.setDwLimitReason("参选单位数量大于允许的最大数量：" + mbJcyjjNum.getDw());
                }
                if (sizeWCR > mbJcyjjNum.getRs()) {
                    jcyjjAllResponse.setRsLimitReason("参选人员数量大于允许的最大数量：" + mbJcyjjNum.getRs());
                }
            }
            if (jcyjjResponse.isSuccess() && jcyjjAllResponse.getTjdjFailReason().isEmpty() && jcyjjAllResponse.getWcryResonseList().isEmpty() && jcyjjAllResponse.getDwLimitReason().isEmpty() && jcyjjAllResponse.getRsLimitReason().isEmpty()) {
                continue;
            }
            jcyjjResponseList.add(jcyjjAllResponse);
        }

        /**
         * 2.技术发明奖：
         */
        for (RbtKjjlJsfmjJbxx jsfmjJbxx : jsfmjJbxxList) {
            JSFMJAllResponse jsfmjAllResponse = new JSFMJAllResponse();
            //技术发明奖的推荐等级的要求：
            String jsfmjJbxxTjdj = jsfmjJbxx.getTjdj();
            int tjdj = Integer.parseInt(jsfmjJbxxTjdj.substring(jsfmjJbxxTjdj.length() - 1));
            Integer zls = mbJsfmjService.lambdaQuery().eq(RbtKjjlMbJsfmj::getDj, String.valueOf(tjdj)).one().getZls();
            String tjdjFailReason = null;
            //各个成果的检验结果：
            JSFMResponse jsfmResponse = jsfmjProcessUtil.processJsfmj(jsfmjJbxx.getId());
            jsfmResponse.setJbxxId(jsfmjJbxx.getId());
//            jsfmResponsesList.add(jsfmResponse);
            //完成人参与项目的处理：
            List<WcryResonse> wcryResonseList = new ArrayList<>();
            List<WcryShortXM> wcryShortXMS = wcrReadUtil.getWcryFilterByTypeAndId(CgjlType.JSFMJ.getCode(), jsfmjJbxx.getId());
            if (wcryShortXMS != null && !wcryShortXMS.isEmpty()) {
                for (WcryShortXM wcry : wcryShortXMS) {
                    WcryResonse wcryResonse = wcrReadUtil.checkWcry(mbTyList, CgjlType.JSFMJ.getCode(), wcry);
                    wcryResonse.setWcryShortXM(wcry);
                    if (!wcryResonse.isSuccess()) {
                        wcryResonseList.add(wcryResonse);
                    }
                }
            }
            //只有推荐单位：
            List<String> dwtjyjList = jsfmjDwtjyjService.lambdaQuery().eq(RbtKjjlJsfmjDwtjyj::getKjjlJsfmjId, jsfmjJbxx.getId()).list().stream().map(e -> e.getTjdwmc()).collect(Collectors.toList());
            //
            int patentCount = jsfmResponse.getPatentCount();
            if (patentCount < zls.intValue()) {
                tjdjFailReason = "推荐等级为：" + TjdjEnum.getByCode(tjdj).getMessage() + "，专利数量少于最低限额：" + zls;
                jsfmjAllResponse.setTjdjFailReason(tjdjFailReason);
            }
            jsfmjAllResponse.setJsfmjJbxx(jsfmjJbxx);
            jsfmjAllResponse.setJsfmResponse(jsfmResponse);
            jsfmjAllResponse.setWcryResonseList(wcryResonseList);
            jsfmjAllResponse.setTjdwList(dwtjyjList);
            this.tjdwNameSet.addAll(dwtjyjList);


            RbtKjjlMbJsfmjNum mbJsfmjNum = jsfmjNumMap.get(String.valueOf(tjdj));
            if (wcryShortXMS != null) {
                int sizeWCR = wcryShortXMS.size();
                if (sizeWCR > mbJsfmjNum.getRs()) {
                    jsfmjAllResponse.setRsLimitReason("参选人员数量大于允许的最大数量：" + mbJsfmjNum.getRs());
                }
            }
            if (jsfmResponse.isSuccess() && jsfmjAllResponse.getTjdjFailReason().isEmpty() && jsfmjAllResponse.getWcryResonseList().isEmpty() && jsfmjAllResponse.getRsLimitReason().isEmpty()) {
                continue;
            }
            jsfmResponsesList.add(jsfmjAllResponse);
        }

        /**
         * 3.科技进步奖：
         */
        for (RbtKjjlKjjbjJbxx kjjbjJbxx : kjjbjJbxxList) {
            KJJBAllResponse kjjbAllResponse = new KJJBAllResponse();
            //各个成果的检验结果：
            KJJBJResponse kjjbjResponse = kjjbjProcessUtil.processKjjbj(kjjbjJbxx.getId());
            kjjbjResponse.setJbxxId(kjjbjJbxx.getId());
//            kjjbjResponseList.add(kjjbjResponse);
            //完成人参与项目的处理：
            List<WcryResonse> wcryResonseList = new ArrayList<>();
            List<WcryShortXM> wcryShortXMS = wcrReadUtil.getWcryFilterByTypeAndId(CgjlType.KJJBJ.getCode(), kjjbjJbxx.getId());
            if (wcryShortXMS != null && !wcryShortXMS.isEmpty()) {
                for (WcryShortXM wcry : wcryShortXMS) {
                    WcryResonse wcryResonse = wcrReadUtil.checkWcry(mbTyList, CgjlType.KJJBJ.getCode(), wcry);
                    wcryResonse.setWcryShortXM(wcry);
                    if (!wcryResonse.isSuccess()) {
                        wcryResonseList.add(wcryResonse);
                    }
                }
            }
            //推荐单位和完成单位：
            List<String> dwtjyjList = kjjbjDwtjyjService.lambdaQuery().eq(RbtKjjlKjjbjDwtjyj::getKjjlKjjbjId, kjjbjJbxx.getId()).list().stream().map(e -> e.getTjdwmc()).collect(Collectors.toList());
            List<String> wcdwList = kjjbjWcdwService.lambdaQuery().eq(RbtKjjlKjjbjWcdw::getKjjlKjjbjId, kjjbjJbxx.getId()).list().stream().map(e -> e.getDwmc()).collect(Collectors.toList());

            kjjbAllResponse.setTjdwList(dwtjyjList);
            this.tjdwNameSet.addAll(dwtjyjList);
            this.wcdwNameSet.addAll(wcdwList);
            kjjbAllResponse.setWcdwList(wcdwList);
            kjjbAllResponse.setKjjlKjjbjJbxx(kjjbjJbxx);
            kjjbAllResponse.setKjjbjResponse(kjjbjResponse);
            kjjbAllResponse.setWcryResonseList(wcryResonseList);


            String tjdj = kjjbjJbxx.getTjdj();
            tjdj = tjdj.substring(tjdj.length() - 1);
            RbtKjjlMbKjjbjNum kjjbjNum = kjjbjNumMap.get(tjdj);
            if (wcryShortXMS != null) {
                int sizeWCR = wcryShortXMS.size();
                int sizeWCDW = wcdwList.size();
                if (sizeWCDW > kjjbjNum.getDw()) {
                    kjjbAllResponse.setDwLimitReason("参选单位数量大于允许的最大数量：" + kjjbjNum.getDw());
                }
                if (sizeWCR > kjjbjNum.getRs()) {
                    kjjbAllResponse.setRsLimitReason("参选人员数量大于允许的最大数量：" + kjjbjNum.getRs());
                }
            }
            if (kjjbjResponse.isSuccess() && kjjbAllResponse.getWcryResonseList().isEmpty() && kjjbAllResponse.getDwLimitReason().isEmpty() && kjjbAllResponse.getRsLimitReason().isEmpty()) {
                continue;
            }

            kjjbjResponseList.add(kjjbAllResponse);
        }
        /**
         * 4.一线难题攻关奖：
         */
        /*
        for (RbtKjjlYxntggjJbxx yxntggjJbxx : yxntggjJbxxList) {

            YXNTGGAllResponse yxntggjAllResponse = new YXNTGGAllResponse();
            //完成人参与项目的处理：
            List<WcryShortXM> wcryShortXMS = wcrReadUtil.getWcryFilterByTypeAndId(CgjlType.YXNTGGJ.getCode(), yxntggjJbxx.getId());
            //获取推荐单位：
            String tjdwId = yxntggjJbxx.getTjdwId();
            List<String> dwtjyjList = formOrgService.lambdaQuery().eq(ZtUpmFormOrg::getBmbh, tjdwId).list().stream().map(e -> e.getBmmc()).collect(Collectors.toList());
            this.tjdwNameSet.addAll(dwtjyjList);
            yxntggjAllResponse.setYxntggjJbxx(yxntggjJbxx);
            yxntggjAllResponse.setTjdwList(dwtjyjList);

            String yxntggJbxxTjdj = yxntggjJbxx.getTjdj();
            int tjdj = Integer.parseInt(yxntggJbxxTjdj.substring(yxntggJbxxTjdj.length() - 1));
            RbtKjjlMbYxntggjNum yxntggjNum = yxntggjNumMap.get(tjdj);

            if (wcryShortXMS != null) {
                int sizeWCR = wcryShortXMS.size();
                if (sizeWCR > yxntggjNum.getRs()) {
                    yxntggjAllResponse.setRsLimitReason("参选人员数量大于允许的最大数量：" + yxntggjNum.getRs());
                }
            }
            if (yxntggjAllResponse.getRsLimitReason().isEmpty()) {
                continue;
            }
            yxntggResponseList.add(yxntggjAllResponse);

        }
        */

        /**
         * 5.杰出成就奖：
         */
        for (RbtKjjlJcyjjJbxx jcyjjJbxx : jcyjjJbxxList) {
            JCYJJAllResponse jcyjjAllResponse = new JCYJJAllResponse();

        }


        //构造PDF的请求体：
        PDFRequest pdfRequest = new PDFRequest();
        pdfRequest.setNd(query.getNd());
        pdfRequest.setTjdwNameSet(tjdwNameSet);
        pdfRequest.setWcdwNameSet(wcdwNameSet);
        pdfRequest.setSizeJCYJJ(jcyjjJbxxList.size());
        pdfRequest.setSizeJSFMJ(jsfmjJbxxList.size());
        pdfRequest.setSizeKJJBJ(kjjbjJbxxList.size());
        pdfRequest.setSizeYXNTGG(yxntggjJbxxList.size());
        pdfRequest.setJcyjjResponseList(jcyjjResponseList);
        pdfRequest.setJsfmResponsesList(jsfmResponsesList);
        pdfRequest.setKjjbjResponseList(kjjbjResponseList);
        pdfRequest.setYxntggResponseList(yxntggResponseList);
        PDFRequest initPDFData = initPDFData(pdfRequest);
        pdfRequest = initPDFData;

        File file = pdfUtil.generatePDF(pdfRequest);
        ZtComUploadFile comUploadFile = new ZtComUploadFile();
        LocalDateTime now = LocalDateTime.now();
        String FileName = "形式审查" + query.getNd().substring(0, 4) + "年度形式审查报告-" + now.format(DATE_FORMAT) + ".pdf";
        comUploadFile.setBatchId("xsscbg");
        comUploadFile.setFileName(FileName);
        comUploadFile.setFileType("application/pdf");
        comUploadFile.setFileMd5(pdfUtil.getPdfName());
        comUploadFile.setCreateUserCode("system");
        comUploadFile.setCreateTime(now);
        comUploadFile.setIsUsing("0");
        comUploadFileService.save(comUploadFile);

        ZtComUploadFileOrigin comUploadFileOrigin = new ZtComUploadFileOrigin();
        comUploadFileOrigin.setId(pdfUtil.getPdfName());
        comUploadFileOrigin.setFileName(FileName);
        comUploadFileOrigin.setFileType("application/pdf");
        comUploadFileOrigin.setFilePath(pdfUtil.getFilePath());
        comUploadFileOrigin.setFileSize(file.length());
        comUploadFileOrigin.setCreateUserCode("system");
        comUploadFileOrigin.setCreateTime(now);
        comUploadFileOriginService.save(comUploadFileOrigin);

        RbtFileXsscbg rbtFileXsscbg = new RbtFileXsscbg();
        rbtFileXsscbg.setNd(query.getNd());
        rbtFileXsscbg.setUserCode(query.getUserCode());
        rbtFileXsscbg.setOriginFileId(comUploadFileOrigin.getId());
        rbtFileXsscbg.setCreateTime(now);
        xsscbgService.save(rbtFileXsscbg);

        log.info("形式审查报告信息已经入库");
        log.info("形式审查报告入库成功，给用户 {} 发送消息", query.getUserCode());

        //发送消息提醒用户查重已经结束：
        imageExtractionUtil.HttpClientServiceSuccess(query, file, FileName);
    }

    public PDFRequest initPDFData(PDFRequest pdfRequest) {

        Map<Integer, CommonShortXM> idToXM = new HashMap<>();
        Map<String, Set<Integer>> tjdwToXmId = new HashMap<>();
        Map<String, Set<Integer>> wcdwToXmId = new HashMap<>();

        List<JCYJJAllResponse> jcyjjResponseList = pdfRequest.getJcyjjResponseList();
        List<JSFMJAllResponse> jsfmResponsesList = pdfRequest.getJsfmResponsesList();
        List<KJJBAllResponse> kjjbjResponseList = pdfRequest.getKjjbjResponseList();
        List<YXNTGGAllResponse> yxntggResponseList = pdfRequest.getYxntggResponseList();
        int cnt = 1;

        //1、基础研究奖
        for (JCYJJAllResponse jcyjjAllResponse : jcyjjResponseList) {
            CommonShortXM commonShortXM = getCommonShortXM(CgjlType.JCYJJ.getCode(), jcyjjAllResponse.getJcyjjJbxx(), null, null,null);
            jcyjjAllResponse.setId(cnt);
            List<String> tjdwList = jcyjjAllResponse.getTjdwList();
            for (String tjdw : tjdwList) {
                if (!tjdwToXmId.containsKey(tjdw)) {
                    Set<Integer> list = new HashSet<>();
                    list.add(cnt);
                    tjdwToXmId.put(tjdw, list);
                } else {
                    tjdwToXmId.get(tjdw).add(cnt);
                }
            }
            List<String> wcdwList = jcyjjAllResponse.getWcdwList();
            for (String wcdw : wcdwList) {
                if (!wcdwToXmId.containsKey(wcdw)) {
                    Set<Integer> list = new HashSet<>();
                    list.add(cnt);
                    wcdwToXmId.put(wcdw, list);
                } else {
                    wcdwToXmId.get(wcdw).add(cnt);
                }
            }
            commonShortXM.setTjdwList(tjdwList);
            commonShortXM.setWcdwList(wcdwList);
            idToXM.put(cnt++, commonShortXM);
        }
        //2、技术发明奖
        for (JSFMJAllResponse jsfmjAllResponse : jsfmResponsesList) {
            CommonShortXM commonShortXM = getCommonShortXM(CgjlType.JSFMJ.getCode(), null, jsfmjAllResponse.getJsfmjJbxx(), null,null);
            jsfmjAllResponse.setId(cnt);
            List<String> tjdwList = jsfmjAllResponse.getTjdwList();
            for (String tjdw : tjdwList) {
                if (!tjdwToXmId.containsKey(tjdw)) {
                    Set<Integer> list = new HashSet<>();
                    list.add(cnt);
                    tjdwToXmId.put(tjdw, list);
                } else {
                    tjdwToXmId.get(tjdw).add(cnt);
                }
            }
            commonShortXM.setTjdwList(tjdwList);
            idToXM.put(cnt++, commonShortXM);
        }
        //3、科技进步奖
        for (KJJBAllResponse kjjbAllResponse : kjjbjResponseList) {
            CommonShortXM commonShortXM = getCommonShortXM(CgjlType.KJJBJ.getCode(), null, null, kjjbAllResponse.getKjjlKjjbjJbxx(),null);
            kjjbAllResponse.setId(cnt);
            List<String> tjdwList = kjjbAllResponse.getTjdwList();
            for (String tjdw : tjdwList) {
                if (!tjdwToXmId.containsKey(tjdw)) {
                    Set<Integer> list = new HashSet<>();
                    list.add(cnt);
                    tjdwToXmId.put(tjdw, list);
                } else {
                    tjdwToXmId.get(tjdw).add(cnt);
                }
            }

            List<String> wcdwList = kjjbAllResponse.getWcdwList();
            for (String wcdw : wcdwList) {
                if (!wcdwToXmId.containsKey(wcdw)) {
                    Set<Integer> list = new HashSet<>();
                    list.add(cnt);
                    wcdwToXmId.put(wcdw, list);
                } else {
                    wcdwToXmId.get(wcdw).add(cnt);
                }
            }
            commonShortXM.setTjdwList(tjdwList);
            commonShortXM.setWcdwList(wcdwList);
            idToXM.put(cnt++, commonShortXM);
        }
        //4、一线难题攻关奖
        for (YXNTGGAllResponse yxntggAllResponse : yxntggResponseList) {
            CommonShortXM commonShortXM = getCommonShortXM(CgjlType.YXNTGGJ.getCode(), null, null, null,yxntggAllResponse.getYxntggjJbxx());
            yxntggAllResponse.setId(cnt);
            List<String> tjdwList = yxntggAllResponse.getTjdwList();
            for (String tjdw : tjdwList) {
                if (!tjdwToXmId.containsKey(tjdw)) {
                    Set<Integer> list = new HashSet<>();
                    list.add(cnt);
                    tjdwToXmId.put(tjdw, list);
                } else {
                    tjdwToXmId.get(tjdw).add(cnt);
                }
            }
            commonShortXM.setTjdwList(tjdwList);
            idToXM.put(cnt++, commonShortXM);
        }

        pdfRequest.setIdToXM(idToXM);
        pdfRequest.setTjdwToXmId(tjdwToXmId);
        pdfRequest.setWcdwToXmId(wcdwToXmId);
        return pdfRequest;
    }


    public CommonShortXM getCommonShortXM(int type, RbtKjjlJcyjjJbxx jcyjjJbxx, RbtKjjlJsfmjJbxx jsfmjJbxx, RbtKjjlKjjbjJbxx kjjlKjjbjJbxx, RbtKjjlYxntggjJbxx yxntggjJbxx) {

        CommonShortXM commonShortXM = new CommonShortXM();
        String jbxxID = null;
        String xmmc = null;
        String tjdj = null;
        LocalDateTime xmBeginTime = null;
        LocalDateTime xmEndTime = null;
        List<String> tjdwList = null;
        List<String> wcdwList = null;
        switch (type) {
            case 0:
                tjdwList = jcyjjDwtjyjService.lambdaQuery().eq(RbtKjjlJcyjjDwtjyj::getKjjlJcyjjId, jcyjjJbxx.getId()).list().stream().filter(e -> e.getTjdwmc() != null).map(e -> e.getTjdwmc()).collect(Collectors.toList());
                wcdwList = jcyjjWcdwService.lambdaQuery().eq(RbtKjjlJcyjjWcdw::getKjjlJcyjjId, jcyjjJbxx.getId()).list().stream().filter(e -> e.getDwmc() != null).map(e -> e.getDwmc()).collect(Collectors.toList());
                jbxxID = jcyjjJbxx.getId();
                xmmc = jcyjjJbxx.getBjmc();
                if (jcyjjJbxx.getXmzqq() != null) {
                    xmBeginTime = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqq());
                }
                if (jcyjjJbxx.getXmzqz() != null) {
                    xmEndTime = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqz());
                }
                tjdj = jcyjjJbxx.getTjdj();
                break;
            case 1:
                tjdwList = jsfmjDwtjyjService.lambdaQuery().eq(RbtKjjlJsfmjDwtjyj::getKjjlJsfmjId, jsfmjJbxx.getId()).list().stream().filter(e -> e.getTjdwmc() != null).map(e -> e.getTjdwmc()).collect(Collectors.toList());
                jbxxID = jsfmjJbxx.getId();
                xmmc = jsfmjJbxx.getXmmc();
                if (jsfmjJbxx.getXmzqq() != null) {
                    xmBeginTime = DateFormatterUtil.StrToLocalDateTime(jsfmjJbxx.getXmzqq());
                }
                if (jsfmjJbxx.getXmzqz() != null) {
                    xmEndTime = DateFormatterUtil.StrToLocalDateTime(jsfmjJbxx.getXmzqz());
                }
                tjdj = jsfmjJbxx.getTjdj();
                break;
            case 2:
                tjdwList = kjjbjDwtjyjService.lambdaQuery().eq(RbtKjjlKjjbjDwtjyj::getKjjlKjjbjId, kjjlKjjbjJbxx.getId()).list().stream().filter(e -> e.getTjdwmc() != null).map(e -> e.getTjdwmc()).collect(Collectors.toList());
                wcdwList = kjjbjWcdwService.lambdaQuery().eq(RbtKjjlKjjbjWcdw::getKjjlKjjbjId, kjjlKjjbjJbxx.getId()).list().stream().filter(e -> e.getDwmc() != null).map(e -> e.getDwmc()).collect(Collectors.toList());
                jbxxID = kjjlKjjbjJbxx.getId();
                xmmc = kjjlKjjbjJbxx.getXmmc();
                if (kjjlKjjbjJbxx.getXmzqq() != null) {
                    xmBeginTime = DateFormatterUtil.StrToLocalDateTime(kjjlKjjbjJbxx.getXmzqq());
                }
                if (kjjlKjjbjJbxx.getXmzqz() != null) {
                    xmEndTime = DateFormatterUtil.StrToLocalDateTime(kjjlKjjbjJbxx.getXmzqz());
                }
                tjdj = kjjlKjjbjJbxx.getTjdj();
                break;
            case 3:
                String tjdwId = yxntggjJbxx.getTjdwId();
                tjdwList = formOrgService.lambdaQuery().eq(ZtUpmFormOrg::getBmbh, tjdwId).list().stream().map(e -> e.getBmmc()).collect(Collectors.toList());
                jbxxID = yxntggjJbxx.getId();
                xmmc = yxntggjJbxx.getBjmc();
                //项目的开始和结束的时间：
                if (yxntggjJbxx.getStartRq() != null){
                    xmBeginTime = DateFormatterUtil.StrToLocalDateTime(yxntggjJbxx.getStartRq());
                }
                if (yxntggjJbxx.getEndRq() != null){
                    xmEndTime = DateFormatterUtil.StrToLocalDateTime(yxntggjJbxx.getEndRq());
                }
                tjdj = yxntggjJbxx.getTjdj();
                break;
        }
        commonShortXM.setJbxxID(jbxxID);
        commonShortXM.setNd(null);
        commonShortXM.setCgjlType(type);
        commonShortXM.setXmmc(xmmc);
        commonShortXM.setXmBeginTime(xmBeginTime);
        commonShortXM.setXmEndTime(xmEndTime);
        commonShortXM.setTjdj(tjdj);
        commonShortXM.setTjdwList(tjdwList);
        commonShortXM.setWcdwList(wcdwList);
        return commonShortXM;
    }


    //基础研究奖专用 ---- 基础研究奖的推荐等级不同：论文的不同要求
    private String checkTJDJ(RbtKjjlJcyjjJbxx jcyjjJbxx, List<RbtKjjlJcyjjLwxx> jcyjjLwxxes) {
        if (jcyjjLwxxes == null || jcyjjLwxxes.size() == 0) return null;
        String failReason;
        String jcyjjJbxxTjdj = jcyjjJbxx.getTjdj();
        int tjdj = Integer.parseInt(jcyjjJbxxTjdj.substring(jcyjjJbxxTjdj.length() - 1));
        RbtKjjlMbJcyjj mbJcyjj = null;
        for (RbtKjjlMbJcyjj kjjlMbJcyjj : mbReadUtil.getKjjlMbJcyjjsList()) {
            if (Integer.parseInt(kjjlMbJcyjj.getDj()) == (tjdj)) {
                mbJcyjj = kjjlMbJcyjj;
                break;
            }
        }
        //SCI | EI
        Integer sciEi = mbJcyjj.getSciEi();
        Integer dq = mbJcyjj.getDq();
        Integer pt = mbJcyjj.getPt();
        Integer zz = mbJcyjj.getZz();
        int otherNum = 0;
        int SCI_dq = 0;
        int zzNum = jcyjjZzxxService.lambdaQuery().eq(RbtKjjlJcyjjZzxx::getKjjlJcyjjId, jcyjjJbxx.getId()).list().size();

        if (dq == 0) {
            for (RbtKjjlJcyjjLwxx lwxx : jcyjjLwxxes) {
                if (lwxx.getQksy().equals("SCI") || lwxx.getQksy().equals("EI")) {
                    SCI_dq++;
                }
                if (lwxx.getQksy().equals("QT")) {
                    otherNum++;
                }
            }
        } else {
            for (RbtKjjlJcyjjLwxx lwxx : jcyjjLwxxes) {
                if (lwxx.getQksy().equals("SCI") && Integer.parseInt(lwxx.getQkfq()) <= dq) {
                    SCI_dq++;
                }
                if (lwxx.getQksy().equals("QT")) {
                    otherNum++;
                }
            }
        }
        if (zzNum >= zz) return null;
        if (otherNum + SCI_dq >= pt || SCI_dq >= sciEi) {
            return null;
        } else {

            failReason = "推荐等级为:" + TjdjEnum.getByCode(tjdj).getMessage() + ",不满足:";
            if (sciEi == 0) {
                failReason += "① 普通论文数量 >= " + pt + "；② 专著数量 >= " + zz + "两个条件的任何一个。";
            } else {
                if (dq == 0) {
                    failReason += "①SCI +" + dq + "论文数量 >=" + sciEi + "；" + "② 普通论文数量 >= " + pt + "；" + "③ 专著数量 >= " + zz + "三个条件中的任何一个。";
                } else {
                    failReason += "①SCI|EI论文数量 >=" + sciEi + "；" + "② 普通论文数量 >= " + pt + "；" + "③ 专著数量 >= " + zz + "三个条件中的任何一个。";
                }
            }
        }
        return failReason;
    }


}
